package com.atguigu.order.service.impl;


import com.atguigu.order.Order;
import com.atguigu.order.service.OrderService;
import com.atguigu.product.Product;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    RestTemplate restTemplate;

    @Override
    public Order createOrder(Long productId, Long userId) {

        //获取商品总金额
        Product product = getProductFromRemoteWithLoadBalanceAnnotation(productId);
        Order order = new Order();

        order.setTotalAmount(product.getPrice().multiply(new BigDecimal(product.getNum())));;
        order.setId(1L);
        order.setTotalAmount(new BigDecimal("0"));
        order.setUserId(userId);
        order.setNickName("zhangsan");
        order.setAddress("尚硅谷");
        order.setProductList(null);

        //远程查询商品列表
        order.setProductList(Arrays.asList(product));
        return order;
    }

    private Product getProductFromRemoteWithLoadBalanceAnnotation(Long productId){
        String url="http://service-product/product/"+productId;
        Product product = restTemplate.getForObject(url, Product.class);
        return product;
    }

}
